﻿namespace UserInterface
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Windows.Forms;

    using BrainTechLLC;

    public partial class PropertyGridForm : Form
    {
        #region Fields

        protected object _viewing;

        #endregion Fields

        #region Constructors

        public PropertyGridForm()
        {
            InitializeComponent();
        }

        #endregion Constructors

        #region Properties

        public object ViewingObject
        {
            get
            {
                return _viewing;
            }
            set
            {
                if (!_viewing.AreEqual(value))
                {
                    _viewing = value;
                    this.propertyGridView1.ViewingObject = value;
                }
            }
        }

        #endregion Properties

        #region Methods

        public void RefreshExceptions()
        {
            dataGridView1.DataSource = new BindingList<Exception>(Globals.SchedulingEngine.Exceptions);
            //dataGridView1.Refresh();
        }

        void PropertyGridForm_ExceptionOccurred(object sender, ObjectEventArgs<Exception> e)
        {
            RefreshExceptions();
        }

        private void PropertyGridForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            GlobalMonitor.GetInstance().ExceptionOccurred -= PropertyGridForm_ExceptionOccurred;
        }

        private void PropertyGridForm_Load(object sender, EventArgs e)
        {
            GlobalMonitor.GetInstance().ExceptionOccurred += new EventHandler<ObjectEventArgs<Exception>>(PropertyGridForm_ExceptionOccurred);
            RefreshExceptions();
        }

        #endregion Methods
    }
}